package com.sh.data.engine.web.openapi.controller.workspace;

import com.sh.data.engine.application.model.dto.base.PagerResponse;
import com.sh.data.engine.application.model.dto.management.UserDTO;
import com.sh.data.engine.application.model.request.management.ProjectMemberSaveRequest;
import com.sh.data.engine.application.service.management.ProjectUserApplicationService;
import com.sh.data.engine.common.annotation.permissionCheck.PermissionCheckMethod;
import com.sh.data.engine.common.enumDefinition.ProjectStatisticTypeEnum;
import com.sh.data.engine.common.enumDefinition.RoleEnum;
import com.sh.data.engine.domain.annotation.ProjectStatisticExecution;
import com.sh.data.engine.web.webapi.controller.base.response.RestResponseEntity;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author: mengzheng.mhc
 * @date: 2024/12/17 13:38
 */
@Validated
@RestController
@RequestMapping("/openapi/project/member")
@Tag(name = "工作空间成员", description = "工作空间成员相关接口")
public class WorkspaceMemberController {

    @Autowired
    private ProjectUserApplicationService projectUserApplicationService;

    @GetMapping("/list")
    @Operation(summary = "获取工作空间成员列表", description = "该接口用于查询工作空间成员列表")
    @PermissionCheckMethod(roles = {RoleEnum.SUPER_ADMIN, RoleEnum.PROJECT_OWNER, RoleEnum.PROJECT_ADMIN})
    public RestResponseEntity<PagerResponse<UserDTO>> listProjectMembers(
        @Parameter(description = "分页页码", schema = @Schema(defaultValue = "1", minimum = "1"))
        @RequestParam(required = false, defaultValue = "1") Integer pageNum,
        @Parameter(description = "分页大小", schema = @Schema(defaultValue = "10", minimum = "1"))
        @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        var result = projectUserApplicationService.getProjectUser(pageNum, pageSize);
        return RestResponseEntity.success(result);
    }

    @PostMapping("/add")
    @Operation(summary = "添加工作空间用户", description = "该接口用于添加工作空间用户")
    @ProjectStatisticExecution(type = ProjectStatisticTypeEnum.USER)
    @PermissionCheckMethod(roles = {RoleEnum.SUPER_ADMIN, RoleEnum.PROJECT_OWNER, RoleEnum.PROJECT_ADMIN})
    public RestResponseEntity<Integer> createProjectMember(@RequestBody @Validated ProjectMemberSaveRequest param) {
        var result = projectUserApplicationService.addProjectUser(param.getRoleId(), param.getUserName());
        return RestResponseEntity.success(result);
    }

    @PostMapping("/update")
    @Operation(summary = "更新工作空间用户", description = "该接口用于更新工作空间用户")
    @PermissionCheckMethod(roles = {RoleEnum.SUPER_ADMIN, RoleEnum.PROJECT_OWNER, RoleEnum.PROJECT_ADMIN})
    public RestResponseEntity<Integer> updateProjectMember(@RequestBody @Validated ProjectMemberSaveRequest param) {
        var result = projectUserApplicationService.updateProjectUser(param.getUserName(), param.getRoleId());
        return RestResponseEntity.success(result);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除工作空间用户", description = "该接口用于删除工作空间用户")
    @PermissionCheckMethod(roles = {RoleEnum.SUPER_ADMIN, RoleEnum.PROJECT_OWNER, RoleEnum.PROJECT_ADMIN})
    @ProjectStatisticExecution(type = ProjectStatisticTypeEnum.USER)
    public RestResponseEntity<Integer> deleteProjectMember(@RequestParam String userName) {
        var result = projectUserApplicationService.deleteProjectUser(userName);
        return RestResponseEntity.success(result);
    }
}
